﻿using LdtJudge.DataEngine.Common;
using LdtJudge.DataEngine.DataBaseStores;
using LdtJudge.DataEngine.Model.XMLConfigModel.DataStores;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace Ldt.WindowsFormsApp
{
    public partial class SourDataFromcs : UIForm
    {
        DataContext dataContext;
        public SourDataFromcs(DataContext dataContext)
        {
            InitializeComponent();
            this.dataContext = dataContext;
            if (!string.IsNullOrEmpty(dataContext.DataLayerOpt.OptimizationXml.OuterXml)&&!string.IsNullOrEmpty(dataContext.DataLayerOpt.OptimizationXml.OuterXml))
            {
                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.LoadXml(dataContext.DataLayerOpt.OptimizationXml.OuterXml);
                TreeNode root = new TreeNode();
                root.Text = "案件要素中心";
                foreach (XmlNode node in xmlDocument.FirstChild.ChildNodes)
                {
                    Loade(root, node);
                }

                uiTreeView1.Nodes.Add(root);
            }
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        public void Loade(TreeNode root,XmlNode xmlNode) 
        {
            TreeNode node = new TreeNode();
            node.Text = xmlNode.Name;
            if (xmlNode.Attributes!=null && xmlNode.Attributes.Count>0)
            {
                TreeNode treeNodesx = new TreeNode();
                treeNodesx.Text = "属性";
                string id = "";
                id = xmlNode.Attributes["id"].Value;
                foreach (XmlAttribute attvalue in xmlNode.Attributes)
                {
                    if (attvalue.Name!="id")
                    {
                        TreeNode treevalue = new TreeNode();
                        treevalue.Text = attvalue.Name ;
                        treevalue.Name = id+ "：" + attvalue.Value;
                        treeNodesx.Nodes.Add(treevalue);
                    }
                   
                }
                node.Nodes.Add(treeNodesx);
                
            }
            root.Nodes.Add(node);
            if (xmlNode.ChildNodes!=null && xmlNode.ChildNodes.Count>0)
            {
                foreach (XmlNode nodeChild in xmlNode.ChildNodes)
                {
                    Loade(node, nodeChild);
                }
            }
            
        }


        private void uiTreeView1_DoubleClick(object sender, EventArgs e)
        {
            
        }

        private void uiTreeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Node.Name.IndexOf("：") >-1)
            {
                string name = e.Node.Text;
                string id= e.Node.Name.Split('：')[0];
                string text = e.Node.Name.Split('：')[1];
                labname.Text = name;
              
                DataModel dataModel= dataContext.DataLayerOpt.SourdatModleList.FirstOrDefault(a => a.ID == id);
                AttributeInfo attributeInfo = dataModel.AttributesList.FirstOrDefault(a => a.Name == name);
                Source sourcemodel= dataModel.sourceData.sourceList.FirstOrDefault(a => a.IsCheck == "1");
                if (attributeInfo!=null)
                {
                    Source source = attributeInfo.sourceList.FirstOrDefault(a => a.IsCheck == "1");
                    text = source.ExtValue;
                    labexValue.Text = source.ExtValue;
                    labsavevalue.Text = source.SaveValue;
                    labpath.Text = dataModel.XmlPath + "/@" + attributeInfo.Name;
                    uiRichorg.Text= HttpHelp.DeCodeBase64new(65001,source.OriginalText);
                    labindex.Text = source.OriginalLocation;
                    lablen.Text = source.OriginalLocationLen;
                    if (!string.IsNullOrEmpty(source.OriginalLocationLen) && !string.IsNullOrEmpty(source.OriginalLocation)) 
                    {
                        if (int.Parse(source.OriginalLocationLen) != 0 && int.Parse(source.OriginalLocation) != -1)
                        {
                            uiRichorg.Focus();
                            uiRichorg.Select(int.Parse(source.OriginalLocation), int.Parse(source.OriginalLocationLen));
                            uiRichorg.SelectionColor = Color.Red;
                        }
                    }
                    
                    
                    //if (!string.IsNullOrEmpty(source.Location) && source.Location!="-1")
                    //{
                    //    int keyWordIndex = int.Parse(source.Location);
                    //    var searchResult = richTextBoxLog.Find(text, keyWordIndex, RichTextBoxFinds.None);
                    //    if (searchResult == -1)
                    //    {
                    //        if (keyWordIndex == 0)
                    //        {
                               
                    //            return;
                    //        }
                    //        else
                    //        {
                              
                    //            return;
                    //        }
                    //    }
                    //    else
                    //    {
                    //        richTextBoxLog.Focus();
                    //        richTextBoxLog.Select(searchResult, text.Length);
                    //        richTextBoxLog.SelectionColor = Color.Red;
                    //    }
                    //}
                }
                if (sourcemodel!=null)
                {
                   
                    richTextBoxLog.Text = HttpHelp.DeCodeBase64new(65001,sourcemodel.OriginalText);

                    if (!string.IsNullOrEmpty(sourcemodel.OriginalLocationLen) && !string.IsNullOrEmpty(sourcemodel.OriginalLocation))
                    {
                        int index = int.Parse(sourcemodel.OriginalLocation);
                        int len = int.Parse(sourcemodel.OriginalLocationLen);
                        if (index != -1 && len != 0)
                        {
                            //richTextBoxLog.Focus();
                            richTextBoxLog.Select(index, len);
                            richTextBoxLog.SelectionColor = Color.Red;
                        }
                    }

                }
            }
        }

    }
}
